package com.schpro.webexperiment.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * redis 配置
 *
 * @author lqs2
 * @date 2018-12-18, Tue
 */
@Configuration
public class RedisConfig {
    @Bean
    @Primary
    public RedisTemplate<Object, Object> objRedisTemplate(
        RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(getJackson2JsonRedisSerializer(true));
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(getJackson2JsonRedisSerializer(true));
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

//    @Bean
//    public RedisTemplate<String, String> strRedisTemplate(
//            RedisConnectionFactory factory) {
//        StringRedisTemplate template = new StringRedisTemplate(factory);
//
//        template.setKeySerializer(new StringRedisSerializer());
//        template.setValueSerializer(getJackson2JsonRedisSerializer(false));
//        template.afterPropertiesSet();
//        return template;
//    }

//    @Bean
//    public RedisTemplate<String, Foo> redisTemplate2(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Foo> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        // 设置value的序列化规则和 key的序列化规则
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(getJackson2JsonRedisSerializer(true));
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//
//    @Bean
//    public RedisTemplate<String, Map<String, FooMember>> redisTemplate3(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Map<String, FooMember>> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(redisConnectionFactory);
//        // 设置value的序列化规则和 key的序列化规则
//        redisTemplate.setKeySerializer(new StringRedisSerializer());
//        redisTemplate.setValueSerializer(getJackson2JsonRedisSerializer());
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }


    private static Jackson2JsonRedisSerializer getJackson2JsonRedisSerializer(boolean obj) {
        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer;
        if (obj) {
            jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        } else {
            jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(String.class);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

}
